package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.JsonResult;
import com.hualan.bean.PageResult;
import com.hualan.bean.PanghuCourierStation;
import com.hualan.bean.PanghuShop;
import com.hualan.dao.StoreDao;
import com.hualan.dao.impl.StoreDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/find")
public class StoreFindByPage extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 接收请求，响应数据
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        String shopName = req.getParameter("shopName");
        String shopPhone = req.getParameter("shopPhone");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");

        // 处理分页查询
        if (pageNum == null || "".equals(pageNum)){
            pageNum = "1";
        }
        if (pageSize == null || "".equals(pageSize)){
            pageSize = "5";
        }


        // 新建courierStationListDao对象
      StoreDao storeDao = new StoreDaoImpl();
        // 创建列表存储查询数据
        List<PanghuShop> panghuShopList = new ArrayList<>();
        Long total = null; // 总条数

        //sql语句中 ： 分页  limit 起始位置,条数   （当前页-1）* 每页显示条数    1页
        int pNum =(Integer.valueOf(pageNum) - 1) * Integer.valueOf(pageSize);

        // 当没有搜索条件时，走此处
        if ((shopName == null || "".equals(shopName)) && (shopPhone == null || "".equals(shopPhone)) && (startTime == null || "".equals(startTime)) && (endTime == null || "".equals(endTime))){
            // 调用dao层查询方法
            panghuShopList = storeDao.panghuShopList(pNum + "", pageSize, shopName, shopPhone,  startTime, endTime);

            // 获取查询总条数
            total = storeDao.selectAll();
            System.out.println("走了这处方法" + total);
        }else {
            // 调用dao层查询方法
           panghuShopList = storeDao.panghuShopList(pNum + "", pageSize, shopName, shopPhone,  startTime, endTime);

            // 获取查询总条数
            total = Long.valueOf(panghuShopList.size());
        }

        PageResult pageResult = new PageResult(Integer.valueOf(pageNum), Integer.valueOf(pageSize), (int) Math.ceil(total.intValue() / Integer.valueOf(pageSize)), total.intValue(), panghuShopList);

        // 查询结果
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(pageResult);
        jsonResult.setStatus(200);

        // 处理数据转化为json
        String jsonString = JSON.toJSONString(jsonResult);
        System.out.println("这是转换后要传给前端的json文件" + jsonString);

        // 响应数据
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(jsonString);



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
